package com.blb.goods_service.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.goods_service.entity.Brand;
import com.blb.goods_service.entity.JsonResult;
import com.blb.goods_service.service.IBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类控制器
 */
@Api("品牌")
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private IBrandService brandService;

    @ApiOperation("分页查询")
    @GetMapping("/page/{page}")
    public JsonResult page(@PathVariable("page")Long page){
        try {
//            IPage<Brand> args = new Page<>(page,10);
//            IPage<Brand> result = brandService.page(args);
            IPage<Brand> result = brandService.pageBrands(page);
            return new JsonResult(1,result);
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(0,null);
        }
    }

    @ApiOperation("全部查询")
    @GetMapping
    public JsonResult list(){
        try {
            List<Brand> result = brandService.list();
            return new JsonResult(1,result);
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(0,null);
        }
    }

    @ApiOperation("根据分类id查询")
    @GetMapping("/category/{cid}")
    public JsonResult findBrandsByCategory(@PathVariable("cid") Integer cid){
        try {
            List<Brand> result = brandService.findBrandsByCategory(cid);
            return new JsonResult(1,result);
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(0,null);
        }
    }

    @ApiOperation("保存")
    @PostMapping
    public JsonResult save(@RequestBody Brand brand){
        System.out.println(brand);
        try {
            brandService.save(brand);
            return new JsonResult(1,brand);
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(0,null);
        }
    }

}
